package cn.iocoder.yudao.module.job;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.pay.api.order.PayOrderApi;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderRespDTO;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.buiessorder.BuiessOrderDO;
import cn.iocoder.yudao.module.peizhen.dal.mysql.buiessorder.BuiessOrderMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class ExpireBusinessOrder implements JobHandler {

    @Resource
    private BuiessOrderMapper buiessOrderMapper;

    @Resource
    private PayOrderApi payOrderApi;
    @Override
    public String execute(String param) throws Exception {
        int count =0;
        List<PayOrderRespDTO> closeOrderList = payOrderApi.getCloseOrderList();

        if (CollUtil.isEmpty(closeOrderList)) {
            return StrUtil.format("支付过期 {} 个", count);
        }
        for (PayOrderRespDTO order : closeOrderList) {
            BuiessOrderDO buiessOrder =buiessOrderMapper.selectOne(BuiessOrderDO::getNo,order.getMerchantOrderId());
            buiessOrder.setStatus(4);
            buiessOrderMapper.updateById(buiessOrder);
        }
        return StrUtil.format("支付过期 {} 个", count);
    }
}
